﻿using System;
using Windows.Networking;
using Windows.Networking.Connectivity;
using System.Collections.Generic;
using Beyondsoft.Tuanju.DataManager;
using Tuan.Common;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.Networking.Sockets;
using Windows.ApplicationModel.DataTransfer;
using Windows.UI.Xaml.Navigation;
using Windows.Foundation;

namespace Tuan
{
    public sealed partial class ItemDetailsPage : LayoutAwarePage
    {
        DataTransferManager dataTransferManager;

        /// <summary>
        /// Initialize ItemDetailsPage
        /// </summary>
        public ItemDetailsPage()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// Binding context data when loading this page
        /// </summary>
        /// <param name="navigationParameter">Navigation parameter</param>
        /// <param name="pageState">Page state</param>
        protected override async void LoadState(object navigationParameter, Dictionary<string, object> pageState)
        {
            this.CityName.DataContext = City.DefaultCity;
            Deal deal = await City.DefaultCity.GetDeal((String)navigationParameter);
            this.dealInfoPanel.DataContext = deal;

            this.dataTransferManager = DataTransferManager.GetForCurrentView();
            this.dataTransferManager.DataRequested += new TypedEventHandler<DataTransferManager, DataRequestedEventArgs>(OnDataRequested);
        }

        /// <summary>
        /// Deleted delegate when leave this page
        /// </summary>
        /// <param name="e"></param>
        protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
        {
            this.dataTransferManager.DataRequested -= new TypedEventHandler<DataTransferManager, DataRequestedEventArgs>(OnDataRequested);
        }

        /// <summary>
        /// Handle shared data 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnDataRequested(DataTransferManager sender, DataRequestedEventArgs e) 
        {
            string appPrefix = "团聚" + CityName.Text+"-";
            string predefinedDiscription = appPrefix + "欢迎您，更多团购等你来选择";
            string failedShareMessage = "分享失败，没有合适的分享信息。";

            string dataPackageTitle = appPrefix + tbShortTitle.Text;

            string dataPackageDiscription = tbTitle.Text;
            if (dataPackageDiscription == null) 
            {
                dataPackageDiscription = predefinedDiscription;
            }

            DataPackage requestData = e.Request.Data;
            requestData.Properties.Title = dataPackageTitle;
            requestData.Properties.Description = dataPackageDiscription;

            Uri sharedDealLink = hbDealLink.NavigateUri;
            if (sharedDealLink != null)
            {
                requestData.SetUri(sharedDealLink);
            }
            else 
            {
                e.Request.FailWithDisplayText(failedShareMessage);
            }
        }

        /// <summary>
        /// Show decription panel when click "概述"
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">agrument</param>
        private void DescriptionButton_Click(object sender, RoutedEventArgs e)
        {
            Canvas.SetZIndex(this.Discription1, 2);
            Canvas.SetZIndex(this.Discription2, 1); ;
        }

        /// <summary>
        /// Show details panel when click "详细信息"
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">agrument</param>
        private void DetailsButton_Click(object sender, RoutedEventArgs e)
        {
            Canvas.SetZIndex(this.Discription1, 1);
            Canvas.SetZIndex(this.Discription2, 2);
        }

        private void ShowShareButton_Click(object sender, RoutedEventArgs e)
        {
            DataTransferManager.ShowShareUI();
        }
    }
}
